package lumis.portal.presentation.perspective.admin;

import lumis.portal.PortalException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.localization.ILocalizationManager;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.presentation.core.HTMLFragmentComponent;
import lumis.portal.presentation.core.LayoutFilePage;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.CookieUtil;
import lumis.util.IResource;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.util.string.AppendingStringBuffer;

/**
 * Renders the Portal Studio's work pane header.
 *
 * @version $Revision: 13321 $ $Date: 2011-08-08 11:32:13 -0300 (Mon, 08 Aug 2011) $
 * @since 6.0.0
 */
@SuppressWarnings("serial")
public class RenderWorkPaneHeaderComponent extends HTMLFragmentComponent
{
	/**
	 * Creates a new RenderPortalStudioFooterComponent.
	 * @param id the component identifier.
	 * @param autoRenderBodyOnly if {@link #setRenderBodyOnly(boolean)} should
	 * automatically be set in case of layout file tags are to be stripped.
	 * @since 6.0.0
	 */
	public RenderWorkPaneHeaderComponent(String id)
	{
		super(id, false);
	}

	@Override
	public CharSequence getHTMLFragment(MarkupStream markupStream,ComponentTag openTag) 
	{
		try
		{
			SessionConfig sessionConfig = SessionConfig.getCurrentSessionConfig();
			ITransaction transaction = PortalTransactionFactory.getCurrentTransaction();
			LayoutFilePage page = (LayoutFilePage) getPage();
			PageConfig pageConfig = page.getPageWebResource().getPage();
			ChannelConfig channelConfig = ManagerFactory.getChannelManager().get(sessionConfig, pageConfig.getChannelId(), transaction);
			IResource resource = PortalAdminStringResource.getInstance();
			
			ChannelConfig portalAdminCategoryChannelConfig = null;
			
			AppendingStringBuffer result = new AppendingStringBuffer(3000);
			
			ILocalizationManager localizationManager = ManagerFactory.getLocalizationManager();

			result.append("<div class=\"cLumAdminWorkPaneUserBar\"></div>");

			result.append("<div id=\"LumisAdminWorkPaneUserBarLeft\" class=\"cLumAdminWorkPaneUserBarLeft\"></div>");
			result.append("<div id=\"LumisAdminWorkPaneUserBarRight\" class=\"cLumAdminWorkPaneUserBarRight\"><img style=\"float: right;\" title=\"" + localizationManager.localize("STR_HIDE", resource, sessionConfig.getLocale()) + "\" hideTitleText=\"" + localizationManager.localize("STR_HIDE", resource, sessionConfig.getLocale()) + "\" showTitleText=\"" + localizationManager.localize("STR_SHOW", resource, sessionConfig.getLocale())
					+ "\" id=\"LumisAdminShowHideNavigationPane\" class=\"cLumAdminShowHideNavigationPane\" src=\"lumis/portalstudio/client/images/");

			if (portalAdminCategoryChannelConfig == null)
				result.append("category1");
			else if (portalAdminCategoryChannelConfig.getPosition() == 1)
				result.append("category2");
			else if (portalAdminCategoryChannelConfig.getPosition() == 2)
				result.append("category3");
			else
				result.append("category4");

			if("true".equals(CookieUtil.getCookie(((WebRequest)getRequest()).getHttpServletRequest(), "lumNavigationPaneHidden")))
				result.append("/ShowNavigationPane.gif\"/></div>");
			else
				result.append("/HideNavigationPane.gif\"/></div>");

			if (page.getRootChannelId().equals(ChannelConfig.LUMIS_ADMIN_CHANNEL_ID))
			{
				/* do not render anything */
			}
			else if (pageConfig.getType() != PageConfig.PAGE_TYPE_ADMIN)
			{
				result.append("<div id=\"LumisAdminWorkPaneTitle\" class=\"cLumAdminWorkPaneTitle\"  >");
				result.append("<div class=\"cLumAdminContentTitleLeft\"  ></div>");
				result.append("<div class=\"cLumAdminContentTitleRight\" ></div>");
				result.append("<div class=\"cLumAdminContentSubTitle\"><b> ");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_PAGE", resource, sessionConfig.getLocale())));
				result.append(": ");
				result.append(XmlUtil.encodeXml(localizationManager.localize(pageConfig.getName(), resource, sessionConfig.getLocale())));
				result.append("</b></div>");
				result.append("</div>");

				result.append("<div id=\"LumisAdminContentTools\">");
				result.append("<div class=\"cLumAdminContentToolsLeft\" >");
				result.append("<ul>");
				result.append("<li>");
				result.append("<span  id=\"lumEditPageButton\" class=\"cLumAdminToolBarButton\" onclick=\"LumisPortalAdmin.onEditPage()\">" + localizationManager.localize("STR_EDIT", resource, sessionConfig.getLocale()) + "</span>");
				result.append("<span>|</span></li>");
				result.append("<li>");
				result.append("<span id=\"lumSavePageButton\" class=\"cLumAdminToolBarButtonDisabled\" onclick=\"LumisPortalAdmin.onSavePage()\">" + localizationManager.localize("STR_SAVE", resource, sessionConfig.getLocale()) + "</span>");
				result.append("<span>|</span></li>");
				result.append("<li>");
				result.append("<span id=\"lumCancelButton\" class=\"cLumAdminToolBarButtonDisabled\" onclick=\"LumisPortalAdmin.onCancel()\">" + localizationManager.localize("STR_CANCEL", resource, sessionConfig.getLocale()) + "</span>");
				result.append("</li>");
				result.append("</ul>");
				result.append("</div>");
				result.append("<div class=\"cLumAdminContentToolsRight\" >");
				result.append("<ul style=\"white-space: nowrap\">");
				result.append("<li>");
				result.append("<a href=\"javascript:LumisPortalAdmin.onPagePreviewClicked()\"><img id=\"lumPagePreviewButton\" class=\"cLumAdminToolBarButtonImageOff\" src=\"lumis/portal/client/images/LumisPreview.gif\"  alt=\"" + localizationManager.localize("STR_PAGE_PREVIEW", resource, sessionConfig.getLocale()) + "\"/></a>");
				result.append("</li>");
				result.append("<li>");
				result.append("<a href=\"javascript:LumisPortalAdmin.onGridLayoutClicked()\"><img id=\"lumGridLayoutButton\" src=\"lumis/portal/client/images/LumisLayoutGrid.gif\" style=\"margin 0px 7px;\"  alt=\"" + localizationManager.localize("STR_GRID_LAYOUT", resource, sessionConfig.getLocale()) + "\"/></a>");
				result.append("</li>");
				result.append("<li>");
				result.append("<a href=\"javascript:LumisPortalAdmin.onStructureLayoutClicked()\"><img id=\"lumStructureLayoutButton\" src=\"lumis/portal/client/images/LumisInterfaceCaption.gif\"  alt=\"" + localizationManager.localize("STR_STRUCTURE_LAYOUT", resource, sessionConfig.getLocale()) + "\"/></a>");
				result.append("</li>");
				result.append("</ul>");
				result.append("</div>");
				result.append("</div>");
			}
			else
			{
				result.append("<div id=\"LumisAdminWorkPaneTitle\" class=\"cLumAdminWorkPaneTitle\">");
				result.append("<div class=\"cLumAdminContentTitleLeft\"></div>");
				result.append("<div class=\"cLumAdminContentTitleRight\"></div>");
				result.append("<div class=\"cLumAdminContentSubTitle\"><b> Canal");
				result.append(": ");
				result.append(XmlUtil.encodeXml(localizationManager.localize(channelConfig.getName(), resource, sessionConfig.getLocale())));
				result.append("</b></div>");
				result.append("</div>");

				result.append("<div id=\"LumisAdminContentTools\" class=\"cLumAdminContentTools\" style=\"white-space: nowrap\">");
				result.append("<div class=\"cLumAdminContentToolsLeft\">");
				result.append("<ul>");
				result.append("<li>");
				result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_SERVICE_INSTANCES_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
				result.append(PageConfig.PAGE_MAIN);
				result.append("?");
				result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
				result.append("=");
				result.append(channelConfig.getId());
				result.append("&");
				result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
				result.append("=");
				result.append(PageConfig.PAGE_ID_ADMIN_SERVICES);
				result.append("\">");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_SERVICE", resource, sessionConfig.getLocale())));
				result.append("</a>");
				result.append("|</li>");
				result.append("<li >");
				result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
				result.append(PageConfig.PAGE_MAIN);
				result.append("?");
				result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
				result.append("=");
				result.append(channelConfig.getId());
				result.append("&");
				result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
				result.append("=");
				result.append(PageConfig.PAGE_ID_ADMIN_PAGES);
				result.append("\">");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_PAGE", resource, sessionConfig.getLocale())));
				result.append("</a>");
				result.append("|</li>");
				result.append("<li>");
				result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_CHANNELS_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
				result.append(PageConfig.PAGE_MAIN);
				result.append("?");
				result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
				result.append("=");
				result.append(channelConfig.getId());
				result.append("&");
				result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
				result.append("=");
				result.append(PageConfig.PAGE_ID_ADMIN_SUBCHANNELS);
				result.append("\">");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_CHANNEL", resource, sessionConfig.getLocale())));
				result.append("</a>");
				result.append("|</li>");
				result.append("<li>");

				result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_TEMPLATE_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
				result.append(PageConfig.PAGE_MAIN);
				result.append("?");
				result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
				result.append("=");
				result.append(channelConfig.getId());
				result.append("&");
				result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
				result.append("=");
				result.append(PageConfig.PAGE_ID_ADMIN_PAGE_TEMPLATES);
				result.append("\">");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_TEMPLATE", resource, sessionConfig.getLocale())));
				result.append("</a>");
				result.append("|</li>");
				result.append("<li>");
				result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_CHANNELS_TEMPLATE_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
				result.append(PageConfig.PAGE_MAIN);
				result.append("?");
				result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
				result.append("=");
				result.append(channelConfig.getId());
				result.append("&");
				result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
				result.append("=");
				result.append(PageConfig.PAGE_ID_ADMIN_CHANNEL_TEMPLATES);
				result.append("\">");
				result.append(XmlUtil.encodeXml(localizationManager.localize("STR_CHANNELS_TEMPLATE", resource, sessionConfig.getLocale())));

				if (channelConfig.hasLocalGroups())
				{
					result.append("</a>");
					result.append("|</li>");
					result.append("<li>");
					result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_LOCAL_GROUPS_TITLE", resource)) + "\" href=\"");
					result.append(PageConfig.PAGE_MAIN);
					result.append("?");
					result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
					result.append("=");
					result.append(channelConfig.getId());
					result.append("&");
					result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
					result.append("=");
					result.append(PageConfig.PAGE_ID_ADMIN_LOCALGROUPS);
					result.append("\">");
					result.append(XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_LOCAL_GROUPS", resource, sessionConfig.getLocale())));
					result.append("</a>");
					result.append("|</li>");
					result.append("<li>");
					result.append("<a title=\"" + XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_LOCAL_USERS_TITLE", resource, sessionConfig.getLocale())) + "\" href=\"");
					result.append(PageConfig.PAGE_MAIN);
					result.append("?");
					result.append(PortalRequestParameters.PAGE_PARAMETER_CHANNELID);
					result.append("=");
					result.append(channelConfig.getId());
					result.append("&");
					result.append(PortalRequestParameters.PAGE_PARAMETER_PAGEID);
					result.append("=");
					result.append(PageConfig.PAGE_ID_ADMIN_LOCALUSERS);
					result.append("\">");
					result.append(XmlUtil.encodeXml(localizationManager.localize("STR_PAGE_LOCAL_USERS", resource, sessionConfig.getLocale())));
					result.append("</a>");
					result.append("</li>");
				}
				else
				{
					result.append("</a>");
					result.append("</li>");

				}
				result.append("</ul>");
				result.append("</div>");

				result.append("</div>");
			}
			
			return result;
		}
		catch (PortalException e)
		{
			throw new RuntimeException(e);
		}
	}
}
